﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     RequestInfo业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class RequestInfoBLL
    {
        private static RequestInfoBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IRequestInfoDao dao = null;

        private RequestInfoBLL()
        {
            dao = RequestInfoSqlMapDao.Instance;
        }

        public static RequestInfoBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new RequestInfoBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<RequestInfo> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<RequestInfo> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw; 
            }
        }


        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<RequestInfo> GetListByParameters(object obj)
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<RequestInfo> list = dao.SelectListOfConditions(obj);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        public System.Collections.Generic.IList<RequestInfo> GetListByStatusID(int TopCounts,string statusID)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();

                ht.Add("TopCounts", TopCounts);
                ht.Add("StatusID", statusID);
                ht.Add("Conditions", "Order By CreateDate Desc");
                System.Collections.Generic.IList<RequestInfo> list = GetListByParameters( ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        private System.Collections.Generic.IList<RequestInfo> GetListByConditions(object TopCounts, string userId, string conditions)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                ht.Add("UserID", userId);
                ht.Add("Conditions", conditions);

                System.Collections.Generic.IList<RequestInfo> list = dao.SelectListOfConditions(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
        public System.Collections.Generic.IList<RequestInfo> GetListOfAllNewRequest(string statusID)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("StatusID", statusID);
                ht.Add("Conditions", "order by CreateDate desc");

                System.Collections.Generic.IList<RequestInfo> list = dao.SelectListOfConditions(ht);

                return list;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public System.Collections.Generic.IList<RequestInfo> GetListByStatusIDOrder(object TopCounts, string userId)
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<RequestInfo> list = GetListByConditions(TopCounts, userId, "order by StatusID,CreateDate desc");

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="RequestID"></param>
        /// <returns></returns>
        public RequestInfo GetDetail(System.String requestid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(requestid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="requestinfo"></param>
        /// <returns></returns>
        public object Add(RequestInfo requestinfo)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;

            try 
            {
                ///daoManager.BeginTransaction();
                requestinfo.State = (int)Common.Enum.auvState.Enabled;
                requestinfo.IsDel = (int)Common.Enum.auvDel.NotDel;
                requestinfo.CreateDate = DateTime.Now;
                requestinfo.UpdateDate = DateTime.Now;
                requestinfo.BeginDate = DateTime.Now;
                requestinfo.EndDate = DateTime.Now; 

                return dao.Add(requestinfo);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="requestinfo"></param>
        /// <returns></returns>
        public int Update(RequestInfo requestinfo)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(requestinfo);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RequestID"></param>
        /// <returns></returns>
        public int Delete(System.String requestid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(requestid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="requestinfo"></param>
        /// <returns></returns>
        public int Delete(RequestInfo requestinfo)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(requestinfo);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        public IList<RequestInfo> GetRequestInfoListPage(NetPager pager)
        {
            try
            {
                return dao.GetRequestInfoListPage(pager);
            }
            catch
            {

                throw;
            }
        }

        /// <summary>
        /// 分页查询Request数据
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="category"></param>
        /// <param name="username"></param>
        /// <param name="sqlwhere"></param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public IList<RequestInfo> GetPagedList(int pagesize, int pageindex,string sqlwhere, ref int recordcount)
        {
            try
            {
                string orderBy = "CreateDate desc,RequestID desc";
                Model.NetPager pager = new Model.NetPager();
                pager.PageIndex = pageindex;
                pager.TableName = "RequestInfo";
                pager.PrimaryKey = "RequestID";
                pager.Where = sqlwhere;
                pager.Order = orderBy;
                pager.SortType = (int)Common.Enum.SortType.Multi;
                pager.PageSize = pagesize;
                IList<RequestInfo> list = GetRequestInfoListPage(pager);
                recordcount = pager.TotalCount;
                return list;
            }
            catch 
            {
                throw;
            }
        }
    }
}
